package org.as3search.providers.impl {
	/*
	 * as3search - http://code.google.com/p/as3search
	 * Copyright (c) 2009-2010 by respective authors. All rights reserved.
	 *
	 * This library is free software; you can redistribute it and/or modify it under the
	 * terms of the GNU Lesser General Public License as published by the Free Software
	 * Foundation; either version 2.1 of the License, or (at your option) any later
	 * version.
	 *
	 * This library is distributed in the hope that it will be useful, but WITHOUT ANY
	 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
	 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
	 *
	 * You should have received a copy of the GNU Lesser General Public License along
	 * with this library; if not, write to the Free Software Foundation, Inc.,
	 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
	 */
	
	import flash.display.Bitmap;
	
	import mx.collections.ArrayList;
	import mx.collections.IList;
	
	import org.as3search.providers.FlexListSearchProvider;
	import org.as3search.providers.SearchProvider;
	import org.flexunit.Assert;
	
	/**
	 * 
	 */
	public class SearchProviderMappingImplTestCase {
		
		private var classToTestRef : org.as3search.providers.impl.SearchProviderMappingImpl;
		
		public function SearchProviderMappingImplTestCase() {
			
		}
		
		[Before]
		public function setup():void {
			this.classToTestRef = new SearchProviderMappingImpl(IList, FlexListSearchProvider);
		}
		
		[Test]
		public function testCreate():void {
//			simple functionality tests
			var target:IList = new ArrayList();
			var provider:SearchProvider = classToTestRef.create(target);
			Assert.assertNotNull("create() should never normally return null",
				provider);
			Assert.assertEquals("create() failed to set the provider's 'target' property",
				provider.target, target);
//			now tests on error throwing.
			try {
				classToTestRef.create(new Bitmap());
				Assert.fail("create() should throw an error when unable to match provider and target types");
			} catch (e:ArgumentError) {
//				passed!
			}
		}
		
		[Test]
		public function testGetProviderType():void {
			Assert.assertEquals("constructor failed to set the 'providerType' property",
				classToTestRef.providerType, FlexListSearchProvider);
		}
		
		[Test]
		public function testSearchProviderMappingImpl():void {
			try {
				new SearchProviderMappingImpl(null, FlexListSearchProvider);
				Assert.fail("constructor did not throw an error on null 'targetType'");
			} catch (e1:ArgumentError) {
//				pass
			}
			
			try {
				new SearchProviderMappingImpl(IList, null);
				Assert.fail("constructor did not throw an error on null 'providerType'");
			} catch (e2:ArgumentError) {
//				pass
			}
			
			try {
				new SearchProviderMappingImpl(IList, Object);
				Assert.fail("constructor did not throw an error if 'providerType' doesn't implement" +
					" org.as3search.providers.SearchProvider.");
			} catch (e3:ArgumentError) {
//				pass
			}
			
			try {
				new SearchProviderMappingImpl(IList, BadProvider);
				Assert.fail("constructor did not throw an error due to bad provider constructor");
			} catch (e4:ArgumentError) {
//				pass 
			}
		}
		
		[Test]
		public function testGetTargetType():void {
			Assert.assertEquals("constructor failed to set the 'targetType' property",
				classToTestRef.targetType, IList);
		}
	}
}

import org.as3search.providers.SearchProvider;

class BadProvider implements SearchProvider {
	public function BadProvider(value:Object) {
		
	}
	
	public function next():Object { return null; }
	
	public function hasNext():Boolean { return false; }
	
	public function reset():void { }
	
	public function get target():Object { return null; }
	public function set target(value:Object):void { }
	
	public function get length():Number { return 0; }
}